package com.ruoyi.web.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.admin.domain.vo.request.flaw.FlawAddRequest;
import com.ruoyi.admin.domain.vo.request.flaw.FlawPageRequest;
import com.ruoyi.admin.domain.vo.request.flaw.FlawUpdateRequest;
import com.ruoyi.admin.domain.vo.response.flaw.FlawPageResponse;
import com.ruoyi.admin.domain.vo.response.flaw.FlawResponse;
import com.ruoyi.admin.service.FlawService;
import com.ruoyi.common.api.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 瑕疵表(Flaw)Controller类
 *
 * @author jianhan
 * @date 2023-01-31 09:53:25
 */
@Api(value = "后台瑕疵表 Controller", tags = {"后台瑕疵表"})
@RestController
@RequestMapping("/api/flaw")
public class FlawController {

    @Autowired
    private FlawService flawService;

    /**
     * 根据id获取数据
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "根据id获取数据", notes = "根据id获取数据")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "path", dataType = "Long")
    @GetMapping("/{id}")
    public Result<FlawResponse> get(@PathVariable Long id) {
        return flawService.getId(id);
    }


    /**
     * 分页获取瑕疵表列表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "分页获取瑕疵表列表", notes = "分页获取瑕疵表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页码", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "查询数量", paramType = "query", dataType = "Integer")
    })
    @GetMapping
    public Result<FlawPageResponse> pageList(@Valid FlawPageRequest req) {
        return flawService.page(new Page<>(req.getPageNum(), req.getPageSize()), req.getFlawModel());
    }


    /**
     * 添加瑕疵表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "添加瑕疵表", notes = "添加瑕疵表")
    @PostMapping
    public Result add(@RequestBody @Valid FlawAddRequest req) {
        return flawService.add(req.getFlawModel());
    }


    /**
     * 修改瑕疵表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "修改瑕疵表", notes = "修改瑕疵表")
    @PutMapping()
    public Result update(@RequestBody @Valid FlawUpdateRequest req) {
        return flawService.update(req.getFlawModel());
    }

    /**
     * 处理瑕疵
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "处理瑕疵", notes = "处理瑕疵")
    @PostMapping("/dispose")
    public Result dispose(@RequestBody FlawUpdateRequest req) {
        return flawService.dispose(req.getFlawModel());
    }

    /**
     * 删除瑕疵表
     *
     * @author jianhan
     * @date 2023-01-31 09:53:25
     **/
    @ApiOperation(value = "删除瑕疵表", notes = "删除瑕疵表")
    @ApiImplicitParam(name = "ids", value = "id集合,逗号隔开", required = true, paramType = "query", dataType = "String")
    @DeleteMapping("/{ids}")
    public Result update(@PathVariable String ids) {
        Boolean result = flawService.removeByIds(Arrays.asList(ids.split(",")).stream().collect(Collectors.toList()));
        return Result.judge(result);
    }

}
